package com.leyou.page.service.Impl;

import com.leyou.common.utils.BeanHelper;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemFeignClient;
import com.leyou.item.dto.*;
import com.leyou.page.dto.SpecGroupNameDTO;
import com.leyou.page.dto.SpecParamNameDTO;
import com.leyou.page.service.GoodsPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    @Autowired
    private ItemFeignClient itemFeignClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX_SPU = "page:spu:id:";
    private static final String KEY_PREFIX_SKU = "page:sku:id:";
    private static final String KEY_PREFIX_DETAIL = "page:detail:id:";
    private static final String KEY_PREFIX_CATEGORY = "page:category:id:";
    private static final String KEY_PREFIX_BRAND = "page:brand:id:";
    private static final String KEY_PREFIX_SPEC = "page:spec:id:";

    //以下方法用map是为只封装前端需要的参数，方便无需再构建一个新的DTO对象
    //如果构建新的DTO也可以，但是比较麻烦
    @Override
    public String querySpuBySpuID(Long spuId) {
        //查询信息
        SpuDTO spuDTO = itemFeignClient.querySpuBySpuID(spuId);
        //将数据放入map中转化为json
        Map<String, Object> map = new HashMap<>();
        map.put("id", spuDTO.getId());
        map.put("name", spuDTO.getName());
        map.put("categoryIds", spuDTO.getCategoryIds());
        map.put("brandId", spuDTO.getBrandId());
        String json = JsonUtils.toJson(map);
        //存储一份到redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPU + spuId, json);
        return json;
    }





    @Override
    public String querySpuDetailBySpuID(Long spuId) {
        //查询信息
        SpuDetailDTO spuDetailDTO = itemFeignClient.querySpuDetailBySpuID(spuId);
        String json = JsonUtils.toJson(spuDetailDTO);
        //存储一份到redis
        redisTemplate.opsForValue().set(KEY_PREFIX_DETAIL + spuId, json);
        return json;
    }




    @Override
    public String querySkuListBySpuID(Long spuId) {
        //查询信息
        List<SkuDTO> skuDTOList = itemFeignClient.querySkuListBySpuID(spuId);
        String json = JsonUtils.toJson(skuDTOList);
        //存储一份到redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SKU + spuId, json);
        return json;
    }



    @Override
    public String queryCategoriesByIds(List<Long> ids) {
        //查询信息
        List<CategoryDTO> list = itemFeignClient.queryCategoriesByIds(ids);

        List<Map<String, Object>> categoryList = list.stream().map(categoryDTO -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", categoryDTO.getId());
            map.put("name", categoryDTO.getName());
            return map;
        }).collect(Collectors.toList());
        String json = JsonUtils.toJson(categoryList);
        // 存入Redis
        redisTemplate.opsForValue().set(KEY_PREFIX_CATEGORY + ids.get(2), json);
        return json;
    }




    @Override
    public String queryBrandById(Long id) {
        //查询信息
        BrandDTO brandDTO = itemFeignClient.queryBrandById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("id", brandDTO.getId());
        map.put("name", brandDTO.getName());
        String json = JsonUtils.toJson(map);
        // 存入Redis
        redisTemplate.opsForValue().set(KEY_PREFIX_BRAND + id, json);
        return json;
    }




    @Override
    public String querySpecGroupListById(Long categoryId) {
        //查询信息
        List<SpecGroupDTO> list = itemFeignClient.querySpecGroupListById(categoryId);
        List<SpecGroupNameDTO> groupList = new ArrayList<>();
        for (SpecGroupDTO groupDTO : list) {
            SpecGroupNameDTO nameDTO = new SpecGroupNameDTO();
            nameDTO.setName(groupDTO.getName());
            //将List<SpecParamDTO> params转化为List<SpecParamNameDTO> params;
            nameDTO.setParams(BeanHelper.copyWithCollection(groupDTO.getParams(), SpecParamNameDTO.class));
            groupList.add(nameDTO);
        }

        String json = JsonUtils.toJson(groupList);
        // 存入Redis
        redisTemplate.opsForValue().set(KEY_PREFIX_SPEC + categoryId, json);
        return json;
    }
}
